package project.suntray.com.core.net.download;

import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;

import java.io.File;
import java.io.InputStream;

import commerce.zhangyang.com.core.net.callback.IRequest;
import commerce.zhangyang.com.core.net.callback.ISuccess;
import commerce.zhangyang.com.core.util.file.FileUtil;
import okhttp3.ResponseBody;

/**
 * Created by ASUS on 2017/12/19.
 * 主要 将写文件的任务 放入到AsyncTask任务中
 */
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public class SaveFileTask extends AsyncTask<Object,Void,File>{

    private final IRequest REQUEST;
    private final ISuccess SUCCESS;

    public SaveFileTask(IRequest request, ISuccess success) {
        REQUEST = request;
        SUCCESS = success;
    }

    /**
     * 在后台执行 任务
     * @param params
     * @return
     */
    @Override
    protected File doInBackground(Object... params) {
        String downloadDir=(String) params[0];
        String extension = (String) params[1];
        final ResponseBody responseBody=(ResponseBody) params[2];
        final String name = (String) params[3];
        final InputStream inputStream = responseBody.byteStream();
        if(downloadDir==null || downloadDir.equals("")){
            downloadDir="down_loads";
        }

        if(extension==null || extension.equals("")){
            extension="";
        }

        if(name==null){
            return  FileUtil.writeToDisk();
        }
        return null;
    }
}
